package com.kmzx.gen.signs.web;

import com.kmzx.gen.signs.api.DataApi;
import com.kmzx.gen.signs.dto.KVData;
import com.kmzx.gen.signs.dto.KVReq;
import com.kmzx.gen.signs.entity.GenData;
import com.kmzx.gen.signs.events.JRedisMessageListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Project gen_sign
 * @Author: carol
 * @Date: 2020/7/6-21:33
 */
@Controller
public class GetDataController {


    private static Logger logger = LoggerFactory.getLogger(GetDataController.class);


    @Autowired
    DataApi dataApi;


    @ResponseBody
    @RequestMapping(value = "toSign")
    public Integer toSignData(@RequestBody KVReq req, HttpServletRequest request, HttpServletResponse response) {
        logger.info("有数据来了");
        logger.info("req = " + req);

        String data = req.getData();
        String sessionID = req.getSessionId();
        KVData kvData = new KVData();
        kvData.setSid(sessionID).setFromData(data).setOrderCode(req.getOrderCode()).setDataId(req.getDataId());
        dataApi.saveGenDataOrder(kvData);
        return 200;
    }


    @ResponseBody
    @RequestMapping(value = "loop/get")
    public List<GenData> loopGetSignData() {
        List<GenData> dataList = dataApi.findLatestGenData();
        dataApi.batchUse(dataList);
        logger.info("获取了数据");
        return dataList;
    }


    @ResponseBody
    @RequestMapping(value = "loop/push")
    public Integer loopPushHadSignData(@RequestBody List<GenData> req) {
        int data = dataApi.batchUpdateData(req);
        return data;
    }

    @ResponseBody
    @PostMapping(value = "loop/push/one")
    public Integer loopOkSignData(String id, String signedData) {
        int data = dataApi.updateSignedData(id, signedData);
        logger.info("提交了数据");
        return data;
    }

    @ResponseBody
    @PostMapping(value = "loop/push/Error")
    public Integer loopOkSignDataError(String id, String signedData) {
        int data = dataApi.errorSignData(id, signedData);
        logger.info("提交了错误数据");
        return data;
    }




    @ResponseBody
    @RequestMapping(value = "loop/ok")
    public String toReceiveOk(String auth, List<String> ids) {


        return null;
    }

}
